package com.sz.controller.frame;

import com.sz.bean.YijiaBean;
import com.sz.util.ReadWriteProperties;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

;

@Controller
public class PropertiesController extends BaseController<YijiaBean> {

    /** 对应pojo的名字 */
    public static final String POJO_NAME = "yijiaBean";

    /** 权限url前缀 */
    public static final String PERMISSION_URL_PREFIX = PERMISSION_PREFIX + POJO_NAME;

    /** 配置列表 **/
    public static final String PROPERTIES_LIST_URL = PERMISSION_URL_PREFIX + "/list";
    /** 保存配置 **/
    public static final String PROPERTIES_SAVE_URL = PERMISSION_URL_PREFIX + "/save";

    public PropertiesController() {
        super(POJO_NAME);
    }

    /**
     * 配置内容列表
     * 
     */
    @RequestMapping(PROPERTIES_LIST_URL)
    public String list(Model m) {
        ClassLoader classLoader = getClass().getClassLoader();
        URL url = classLoader.getResource("yijia.properties");
        Properties pro = ReadWriteProperties.read(url.getFile());
        m.addAttribute("map", pro);
        return PROPERTIES_LIST_URL;
    }

    /**
     * 保存配置
     * 
     */
    @RequestMapping(PROPERTIES_SAVE_URL)
    public String save(Model m,YijiaBean t) {
        ClassLoader classLoader = getClass().getClassLoader();
        URL url = classLoader.getResource("yijia.properties");
        String path = url.getFile();
        Map<String,String> params=new HashMap<String,String>();
        params.put("appName", t.getAppName());
        params.put("title", t.getTitle());
        ReadWriteProperties.write(path, params);
        m.addAttribute("msg", "ok");
        return list(m);
    }

    @Override
    public String modify(Model m, Long id) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public String remove(Model m, String ids) {
        // TODO Auto-generated method stub
        return null;
    }

}
